﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace gui {

    /// <summary>
    /// Interaction logic for StatusPane.xaml
    /// The status UI component that appears at the bottom of the screen
    /// </summary>
    public partial class StatusPane : UserControl {

        public static bool BusyStatus { get; private set; }

        public StatusPane() {
            InitializeComponent();

            // Attach to StatusInfo
            StatusInfo._Dispatcher = this.Dispatcher;
            this.DataContext = StatusInfo.Instance;

            // Setup Loading animation
            BusyStatus = true;
            LoadReady();

            // Register GUI spinner animation events
            NetworkModelConnect.OnFinishedProcessingEvent += LoadReady;
            NetworkModelConnect.OnConnectingEvent += LoadBusy;
            NetworkModelConnect.OnConnectedEvent += LoadReady;
            NetworkModelConnect.TapeLoadingEvent += LoadBusy;
            Menu.OnResetEvent += LoadBusy;
            NetworkModelConnect.TapeLoadedEvent += LoadReady;
            NetworkModelConnect.OnErrorEvent += LoadReady;

            PlayControl.RunningEvent += LoadBusy;
            PlayControl.SkipToEndEvent += LoadBusy;
            PlayControl.ReadyEvent += LoadReady;
            PlayControl.PausedEvent += LoadReady;
            PlayControl.FinishedEvent += LoadReady;

            ChartTab.RunningEvent += LoadBusy;
            ChartTab.ReadyEvent += LoadReady;
            Graph.RunningEvent += LoadBusy;
            Graph.ReadyEvent += LoadReady;

            // Register event handlers for node selection changed on nodes tab
            NodeListView.NodeSelectedEvent += NodeQueryRunning;
            MessageTotalListView.NodeQueryFinishedEvent += NodeQueryFinished;
            performingNodeSpin = false;
        }

        private void LoadBusy() {
            if (BusyStatus == false) {
                BusyStatus = true;
                this.Dispatcher.Invoke(DispatcherPriority.Normal, (Action)(() => {
                    LoadedXAML.Visibility = Visibility.Collapsed;
                    LoaderXAML.Visibility = Visibility.Visible;
                }));
            }
        }
        private void LoadReady() {
            if (BusyStatus == true) {
                BusyStatus = false;
                this.Dispatcher.Invoke(DispatcherPriority.Normal, (Action)(() => {
                    LoaderXAML.Visibility = Visibility.Collapsed;
                    LoadedXAML.Visibility = Visibility.Visible;
                }));
            }
        }

        // Event handlers for node selection changed on nodes tab
        private bool performingNodeSpin;
        private void NodeQueryRunning() {
            // If the spinner is already spinning, don't perform query spins
            if (BusyStatus == false && performingNodeSpin == false) {
                performingNodeSpin = true;
                LoadBusy();
                StatusInfo.SetStatus("Communicating with Database");
            }
        }
        private void NodeQueryFinished() {
            if (performingNodeSpin) {
                // Reset Status to Ready
                LoadReady();
                StatusInfo.SetStatus("Ready");
                performingNodeSpin = false;
            }
        }

    }
}
